/**********************************************
** @Author: phy
** @Date:   2017-12-12 14:17:37
** @Last Modified by:   phy
***********************************************/
package controllers

import (
	"fmt"
	"strconv"
	"reflect"
	//"strings"
	//"time"

	//"github.com/astaxie/beego"
	"WebAdminSys/libs"
	"WebAdminSys/models"
)

type ExtremevipManageController struct {
	BaseController
}

func (self *ExtremevipManageController) List() {
	self.Data["pageTitle"] = "礼物赠送接收"
	//self.Ctx.WriteString("MangeAdminController.List")
	self.display()
}

func (self *ExtremevipManageController) ExtermevipUserList() {
	self.Data["pageTitle"] = "至尊VIP列表"
	//self.Ctx.WriteString("MangeAdminController.List")
	self.AddOperationLog(fmt.Sprintf("从【%s】界面",self.Data["pageTitle"]),1)
	self.display()
}
func (self *ExtremevipManageController) ExtermevipUserListTable() {
	//列表


	usercondition := self.GetString("d_usercondition");
	userconditioninfo := self.GetString("d_usercondition_info");
	duserclasscondition := self.GetString("d_userclass_condition");


	fmt.Println("-----------------");
	fmt.Println(usercondition,userconditioninfo,duserclasscondition);
	//查询条件
	//filters := make([]interface{}, 0)
	


	//filters = append(filters, "status", 1)
	result, count,_ := models.ExtremVipUserInfo(usercondition,userconditioninfo,duserclasscondition)
	list := make([]map[string]interface{}, len(result))
	var k int64 = 0;
	for _, v := range result {
		row := make(map[string]interface{})
		//if(reflect.TypeOf(v["LVSIGN"]) == nil || reflect.TypeOf(v["USERID"]) == nil || reflect.TypeOf(v["GOLD"]) == nil || reflect.TypeOf(v["INSUREGOLD"]) == nil){
			//continue;
		//}
		row["UserID"] = v["GameID"];
		row["UserNickName"] = v["NickName"];
		row["AutoGraph"] = v["DESCRIPTION"];
		row["WriteTime"] =  v["CreateTime"];
		//fmt.Println(reflect.TypeOf(v["LVSIGN"])," ++++++++++++" )
		//if(v["LVSIGN"].(int64) == 1 ){
		//	row["UserKind"] = "一类";
		//}else if (v["LVSIGN"].(int64) == 2){
			//row["UserKind"] = "二类";
		//}
		row["UserKind"] = v["LVSIGN"];
		row["Glods"] = v["zichan"]; 
		row["Diamonds"] = v["DIAMOND"];//beego.Date(time.Unix(v.CreateTime, 0), "Y-m-d H:i:s")
		row["Platform"] = v["Guarantee"]; //beego.Date(time.Unix(v.UpdateTime, 0), "Y-m-d H:i:s")
		row["LastweekCount"] =v["SellScore"];
		row["GitfCount"] =v["SellCount"];
		// models.ExtremVipUserGitfCountAndLastWeekGold(a)
	
		list[k] = row
		k++;
	}

	self.AddOperationLog(fmt.Sprintf("从【至尊VIP列表】界面昨到查询数据：条件是：%s,%s,%s",usercondition,userconditioninfo,duserclasscondition),1)
	self.ajaxList("成功", MSG_OK, count, list)
}


func (self *ExtremevipManageController) GetTitleDatas() {

	usercondition := self.GetString("d_usercondition");
	userconditioninfo := self.GetString("d_usercondition_info");
	duserclasscondition := self.GetString("d_userclass_condition");
	row := make(map[string]interface{})
	row["alluseScore"] = models.ExtremVipUserAllGold(usercondition,userconditioninfo,duserclasscondition);
	row["SellCount"],row["SellScore"] = models.ExtremVipUserCountSendGold();
	list := make([]map[string]interface{},1)
	list[0] = row
	self.ajaxList("成功", MSG_OK, 1, list)
	
}

func (self *ExtremevipManageController) ExtermeVipUserEdit() {
	self.Data["pageTitle"] = "修改至尊VIP"
	UserID,_ := self.GetInt64("ServerID");

	res := models.ExtremVipUserInfoByUserID(UserID,"NICKNAME","LVSIGN","PLATFORM");
	var iSingin int64 = 0;
	szNickName:= "0"
	var vipPlatform int64 = 0;
	if len(res) > 0 {
		if reflect.TypeOf(res[0]["LVSIGN"]) != nil {
			iSingin = res[0]["LVSIGN"].(int64)
		}
		if reflect.TypeOf(res[0]["NICKNAME"]) != nil {
			szNickName = res[0]["NICKNAME"].(string)
		}
		if reflect.TypeOf(res[0]["PLATFORM"]) != nil {
			vipPlatform = res[0]["PLATFORM"].(int64)
		}	
	}

	self.Data["UserNickName"] =  szNickName;
	self.Data["writedatetime"],self.Data["vipPlatform"] =  models.ExtremVipUserCreateTime(UserID);
	self.Data["LVSIGN"] =  iSingin;
	self.Data["UserID"] =  UserID;
	self.AddOperationLog(fmt.Sprintf("进入【修改至尊VIP】页面，用户信息：%d,%s,%d,%d,%s",UserID,szNickName,iSingin,vipPlatform,self.Data["writedatetime"]),1)
	self.display();
}

func (self *ExtremevipManageController) ModifyVipInfo() {
	UserId,err := self.GetInt("USER_ID");
	if err != nil{
		self.ajaxMsg("失败！",MSG_ERR);
		return
	}
	CurlvSign,err := self.GetInt64("CUR_LVSIGN");
	if err != nil{
		self.ajaxMsg("失败！",MSG_ERR);
		return
	}
	Modifylvsign,err := self.GetInt64("MODFY_LVSIGN");
	if err != nil{
		self.ajaxMsg("失败！",MSG_ERR);
		return
	}
	ModifyVipPlatform := self.GetString("MODFY_VIP_Platform");
	CurVipPlatform := self.GetString("CUR_VIP_Platform");


	err = nil;
	if CurlvSign != Modifylvsign {
			UserCrl := models.UserCtrlInfo{
				USERID:UserId,
				NTYPE:3,
				LVSIGN:Modifylvsign,
			};
		err = UserCrl.UserCtrlInfoInsertValue();
		if err == nil {
			self.AddOperationLog(fmt.Sprintf("将用户【%d】从至尊vip等级【%d】改到【%d】",UserId,CurlvSign,Modifylvsign),1);
	 	}else{
	 		self.AddOperationLog(fmt.Sprintf("将用户【%d】从至尊vip等级【%d】改到【%d】",UserId,CurlvSign,Modifylvsign),0);
	 	}
	}
	tapID,err := self.GetInt64("USER_ID");
	tapCurlvSign,err := self.GetInt("MODFY_LVSIGN");
	if  err == nil {
		vipInfo :=  models.SuperVipInfo{
			UserID:tapID,
			Guarantee:ModifyVipPlatform,
			LVSIGN:tapCurlvSign,
		}
		err = vipInfo.ModefySuperVipInfo();
		if err == nil {
			self.AddOperationLog(fmt.Sprintf("将用户【%d】从至尊vip信息【%d】改到【%d】",UserId,CurVipPlatform,ModifyVipPlatform),1);
	 	}else{
	 		self.AddOperationLog(fmt.Sprintf("将用户【%d】从至尊vip信息【%d】改到【%d】",UserId,CurVipPlatform,ModifyVipPlatform),0);
	 	}

	}
	self.ajaxMsg("成功！",MSG_OK);
	return;
}

func (self *ExtremevipManageController) AjaxDel() {

	
}
func (self *ExtremevipManageController) GiveSendGitfList() {
	self.Data["pageTitle"] = "礼物赠送接收"
	self.AddOperationLog(fmt.Sprintf("进入【%s】页面",self.Data["pageTitle"] ),1);
	self.display();
}

func (self *ExtremevipManageController) GiveSendGitfListTable() {
	
	//列表
	page, err := self.GetInt("page")
	if err != nil {
		page = 1
	}
	limit, err := self.GetInt("limit")
	if err != nil {
		limit = 30
	}
	self.pageSize = limit

    usercondition := self.GetString("d_usercondition");
    userconditioninfo := self.GetString("d_usercondition_info");
    strTime1 := self.GetString("d_befor_date");
    strtime2 := self.GetString("d_dafter_date");

    res,num,_ := models.ExtremVipSendGitfListTable(page,self.pageSize,usercondition,userconditioninfo,strTime1,strtime2 );
	list := make([]map[string]interface{}, len(res))

	for k, v := range res {
		row := make(map[string]interface{})
		row["UserID"] = v["GameID"];
		row["UserNickName"] = v["NickName"];
		row["Platform"] = v["Guarantee"];
		row["WriteTime"] = v["CreateTime"];
		row["SendGiftA"] = v["SellScore"];
		row["GiveGiftB"] = v["BuyScore"];
		row["Conut"] = v["SellCount"];
		row["UserConut"] = v["C"];
		row["dataTmp"] = v["ZSSC"];
		//fmt.Println(reflect.TypeOf(v["SellScore"]),reflect.TypeOf(v["ZSSC"]),reflect.TypeOf(v["SellCount"]),reflect.TypeOf(v["BuyScore"]));
	
		list[k] = row;
	}
	tilteRes,_,_ := models.ExtremVipSendGitfListTableTitle(usercondition,userconditioninfo,strTime1,strtime2 );
	tmpdata := make(map[string]interface{})
	if len(tilteRes) > 0{
		tmpdata["AllSellScore"] = tilteRes[0]["SS"];
		tmpdata["AllSellCount"] = tilteRes[0]["SC"];
		tmpdata["AllZSSC"] = tilteRes[0]["TZSSC"];
		AllSellScore,_ := strconv.ParseInt(tilteRes[0]["SS"].(string), 10, 64)
		AllBuyScore,_ := strconv.ParseInt(tilteRes[0]["BC"].(string), 10, 64)
		tmpdata["ZHB"] = libs.GetTaxRateToSring(AllSellScore,AllBuyScore);	
	}

	//fmt.Println(tmpdata);
	self.AddOperationLog(fmt.Sprintf("在【礼物赠送接收】页面查询数据：%s,%s,%s,%s",usercondition,userconditioninfo,strTime1,strtime2),1);
	self.ajaxTilteList("成功", MSG_OK,num,tmpdata,list)
	//self.display();
}


func (self *ExtremevipManageController) Platformtodaydetail() {
	self.Data["pageTitle"] = "平台当日明细"
	self.AddOperationLog(fmt.Sprintf("进入【%s】页面",self.Data["pageTitle"] ),1);

	value :=  models.ExtremVipPlatformTodayDetail();

	self.Data["D"] = (*value)[0];
	self.display();
}

func (self *ExtremevipManageController) Platformtodaydetaildata() {
	self.Data["pageTitle"] = "平台当日明细"
	self.AddOperationLog(fmt.Sprintf("进入【%s】页面",self.Data["pageTitle"] ),1);
	self.display();
}

func (self *ExtremevipManageController) PlatFromDeTail() {
	self.Data["pageTitle"] = "平台收益明细"
	self.AddOperationLog(fmt.Sprintf("进入【%s】页面",self.Data["pageTitle"] ),1);

	self.display();
}


func (self *ExtremevipManageController) PlatFromDeTailTable() {
	
	//用户信息
	BeforDateTime := self.GetString("BeforDateTime");
	AfterDateTime := self.GetString("AfterDateTime");
	i64TzeroTime :=  libs.GetYesterdayUnixZeroTime();
	
	strsql := "";
		
	if  BeforDateTime != "" {
		i64BeforDateTime := libs.GetStringTimeToUnix(BeforDateTime,"Y-m-d");
		if i64BeforDateTime > i64TzeroTime {
			i64BeforDateTime = i64TzeroTime;
		}
		strsql = fmt.Sprintf("%s AND DataDateTime >= %d ",strsql,i64BeforDateTime);
	}
		
	if  AfterDateTime != "" {
		i64AfterDateTime:= libs.GetStringTimeToUnix(AfterDateTime,"Y-m-d");
		if i64AfterDateTime > i64TzeroTime {
			i64AfterDateTime = i64TzeroTime;
		}

		strsql = fmt.Sprintf("%s AND DataDateTime <= %d",strsql,i64AfterDateTime);
	}

	

	//列表
	page, err := self.GetInt("page")
	if err != nil {
		page = 1
	}
	limit, err := self.GetInt("limit")
	if err != nil {
		limit = 30
	}
	self.pageSize = limit

	result, count,_ := models.PlatFromDeTailTData(page,self.pageSize,strsql);
	list := make([]map[string]interface{}, len(result))
	for k, v := range result {
			row := make(map[string]interface{})

			row["columnA"] = libs.GetDateToString(v["DataDateTime"].(string),"Y-m-d H:i:s","Y-m-d");
			row["columnB"] = v["RegCount"]
			row["columnC"] = v["LogonCount"];
			row["columnD"] = v["RMB"]
			row["columnE"] = v["TopOnLine"]
			row["columnF"] = v["GMAddGold"];
			row["columnG"] = v["SysGold"];
			row["columnH"] = v["UserGold"]
			row["columnI"] = v["VIPGold"]
			row["columnJ"] = v["SellScore"]
			i64SellScore,_ := strconv.ParseInt(v["SellScore"].(string),10,64);
			i64BuyScore,_ := strconv.ParseInt(v["BuyScore"].(string),10,64);
			row["columnK"] = (i64SellScore - i64BuyScore);
			row["columnL"] = v["SysWinLose"]
			row["columnM"] = libs.GetTaxRateToSring(i64BuyScore,i64SellScore)
			row["columnN"] = v["BloodScore"]
			//i64SellCount,_ := strconv.ParseInt(v["SellCount"].(string),10,64);
			//i64BuyCount,_ := strconv.ParseInt(v["BuyCount"].(string),10,64);

			row["columnO"] = v["SellCount"]
			row["columnP"] = v["UserCount"]
			list[k] = row
	}
	TitleData,_,_ := models.PlatFromDeTailTDataTitle(strsql);
	TitleDatalist := make(map[string]interface{})
	if len(TitleData) >0 {
		TitleDatalist["TSWL"] = TitleData[0]["TSWL"]
		TitleDatalist["TRMB"] = TitleData[0]["TRMB"]
		i64SellScore,_ := strconv.ParseInt( TitleData[0]["TSS"].(string),10,64);
		i64BuyScore,_ := strconv.ParseInt( TitleData[0]["TBS"].(string),10,64);

		TitleDatalist["TBSP"] = libs.GetTaxRateToSring(i64BuyScore,i64SellScore)

		TitleDatalist["TGMGD"] = TitleData[0]["TGMGD"]
		TitleDatalist["TSC"] = (i64SellScore - i64BuyScore);
	}
	self.ajaxTilteList("成功", MSG_OK, count,TitleDatalist,list)	
}